﻿import 'dart:developer' as developer;
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import '../../action/questionnaire/routes.dart';
import '../ring/routes.dart';

/// 设置控制器
class SettingsLogic extends GetxController {
  static const String _tag = 'SettingsController';

  // 响应式状态变量
  final RxBool isNotificationEnabled = true.obs;
  final RxBool isDarkMode = false.obs;
  final RxString appVersion = '1.0.0'.obs;
  final RxInt dailyStepsGoal = 10000.obs;
  final RxInt sleepGoalHours = 8.obs;
  final RxInt exerciseGoalMinutes = 30.obs;

  @override
  void onInit() {
    super.onInit();
    _loadSettings();
    developer.log('🎮 设置控制器初始化完成', name: _tag);
  }

  /// 加载设置
  void _loadSettings() {
    // 从本地存储加载设置
    // 这里可以使用 SharedPreferences 或其他持久化方案
    developer.log('📱 加载用户设置', name: _tag);
  }

  /// 切换通知设置
  void toggleNotification(bool value) {
    isNotificationEnabled.value = value;
    developer.log('🔔 通知设置: $value', name: _tag);
    _saveSettings();
    _showSuccessMessage('通知设置已${value ? "开启" : "关闭"}');
  }

  /// 切换深色模式
  void toggleDarkMode(bool value) {
    isDarkMode.value = value;
    // 切换应用主题
    Get.changeThemeMode(value ? ThemeMode.dark : ThemeMode.light);
    developer.log('🌓 深色模式: $value', name: _tag);
    _saveSettings();
    _showSuccessMessage('已切换到${value ? "深色" : "浅色"}模式');
  }

  /// 显示步数目标设置对话框
  void showStepsGoalDialog() {
    developer.log('👟 设置步数目标', name: _tag);
    Get.dialog(
      AlertDialog(
        title: const Text('设置每日步数目标'),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text('当前目标：${dailyStepsGoal.value}步'),
            const SizedBox(height: 16),
            Obx(() => Slider(
              value: dailyStepsGoal.value.toDouble(),
              min: 5000,
              max: 20000,
              divisions: 30,
              label: '${dailyStepsGoal.value}步',
              onChanged: (value) {
                dailyStepsGoal.value = value.toInt();
              },
            )),
          ],
        ),
        actions: [
          TextButton(
            onPressed: () => Get.back(),
            child: const Text('取消'),
          ),
          ElevatedButton(
            onPressed: () {
              _saveSettings();
              Get.back();
              _showSuccessMessage('步数目标已设置为${dailyStepsGoal.value}步');
            },
            child: const Text('确定'),
          ),
        ],
      ),
    );
  }

  /// 显示睡眠目标设置对话框
  void showSleepGoalDialog() {
    developer.log('😴 设置睡眠目标', name: _tag);
    Get.dialog(
      AlertDialog(
        title: const Text('设置睡眠时长目标'),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text('当前目标：${sleepGoalHours.value}小时'),
            const SizedBox(height: 16),
            Obx(() => Slider(
              value: sleepGoalHours.value.toDouble(),
              min: 6,
              max: 10,
              divisions: 8,
              label: '${sleepGoalHours.value}小时',
              onChanged: (value) {
                sleepGoalHours.value = value.toInt();
              },
            )),
          ],
        ),
        actions: [
          TextButton(
            onPressed: () => Get.back(),
            child: const Text('取消'),
          ),
          ElevatedButton(
            onPressed: () {
              _saveSettings();
              Get.back();
              _showSuccessMessage('睡眠目标已设置为${sleepGoalHours.value}小时');
            },
            child: const Text('确定'),
          ),
        ],
      ),
    );
  }

  /// 显示运动目标设置对话框
  void showExerciseGoalDialog() {
    developer.log('🏃 设置运动目标', name: _tag);
    Get.dialog(
      AlertDialog(
        title: const Text('设置运动时长目标'),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text('当前目标：${exerciseGoalMinutes.value}分钟'),
            const SizedBox(height: 16),
            Obx(() => Slider(
              value: exerciseGoalMinutes.value.toDouble(),
              min: 15,
              max: 120,
              divisions: 21,
              label: '${exerciseGoalMinutes.value}分钟',
              onChanged: (value) {
                exerciseGoalMinutes.value = value.toInt();
              },
            )),
          ],
        ),
        actions: [
          TextButton(
            onPressed: () => Get.back(),
            child: const Text('取消'),
          ),
          ElevatedButton(
            onPressed: () {
              _saveSettings();
              Get.back();
              _showSuccessMessage('运动目标已设置为${exerciseGoalMinutes.value}分钟');
            },
            child: const Text('确定'),
          ),
        ],
      ),
    );
  }

  /// 跳转到个人信息页面（问卷流程）
  void navigateToPersonalInfo() {
    developer.log('👤 跳转到健康问卷', name: _tag);
    Get.toNamed(QuestionnaireRoutes.questionnaire);
  }

  /// 跳转到戒指设备设置
  void navigateToRingSettings() {
    developer.log('💍 跳转到戒指设备设置', name: _tag);
    // 跳转到戒指页面
    Get.toNamed(RingRoutes.ring);
  }

  /// 显示数据同步设置
  void showDataSyncSettings() {
    developer.log('🔄 数据同步设置', name: _tag);
    _showComingSoon('数据同步设置');
  }

  /// 显示语言设置对话框
  void showLanguageDialog() {
    developer.log('🌍 语言设置', name: _tag);
    Get.dialog(
      AlertDialog(
        title: const Text('选择语言'),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            ListTile(
              title: const Text('简体中文'),
              leading: const Icon(Icons.check),
              onTap: () {
                Get.back();
                _showSuccessMessage('语言已设置为简体中文');
              },
            ),
            ListTile(
              title: const Text('English'),
              onTap: () {
                Get.back();
                _showComingSoon('English language support');
              },
            ),
          ],
        ),
      ),
    );
  }

  /// 导出健康数据
  void exportHealthData() {
    developer.log('📤 导出健康数据', name: _tag);
    _showComingSoon('健康数据导出');
  }

  /// 检查固件更新
  void checkFirmwareUpdate() {
    developer.log('🔄 检查固件更新', name: _tag);
    _showComingSoon('固件更新检查');
  }

  /// 显示更新历史
  void showUpdateHistory() {
    developer.log('📜 显示更新历史', name: _tag);
    _showComingSoon('更新历史记录');
  }

  /// 跳转到健康报告
  void navigateToHealthReport() {
    developer.log('📊 跳转到健康报告', name: _tag);
    _showComingSoon('健康报告');
  }

  /// 跳转到成就系统
  void navigateToAchievements() {
    developer.log('🏆 跳转到成就系统', name: _tag);
    _showComingSoon('成就系统');
  }

  /// 显示用户协议
  void showUserAgreement() {
    developer.log('📄 显示用户协议', name: _tag);
    _showComingSoon('用户服务协议');
  }

  /// 显示隐私政策
  void showPrivacyPolicy() {
    developer.log('🔒 显示隐私政策', name: _tag);
    _showComingSoon('隐私保护政策');
  }

  /// 显示意见反馈
  void showFeedback() {
    developer.log('💬 显示意见反馈', name: _tag);
    _showComingSoon('意见反馈');
  }

  /// 联系在线客服
  void contactOnlineSupport() {
    developer.log('💬 联系在线客服', name: _tag);
    _showComingSoon('在线客服');
  }

  /// 显示常见问题
  void showFAQ() {
    developer.log('❓ 显示常见问题', name: _tag);
    _showComingSoon('常见问题');
  }

  /// 保存设置
  void _saveSettings() {
    // 保存设置到本地存储
    developer.log('💾 保存用户设置', name: _tag);
  }

  /// 显示成功消息
  void _showSuccessMessage(String message) {
    Toast.show(message);
  }

  /// 显示即将推出消息
  void _showComingSoon(String feature) {
    Toast.show('$feature功能即将推出');
  }
}
